<?php

/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/


class win_gtk {

	private $thread = FALSE;			// Object with info about this thread execution


	var $glade=FALSE;						// Glade XML object
	var  $win=FALSE;						// gtk window obj
	
	var $flags = array();					// misc flags to control sequencing etc.

	public  $win_name = 'unknown';
	public  $win_icon = 'st_green.ico';
	public  $win_title = 'PST';

	public  $const_base=20000;				// Starting value for constants
	public  $const_count=0;					// Increment for each new constant
	
	public $control=array();				// All controls

	public  $win_parent=NULL;				// winbinder window obj parent window

	var $dbh = FALSE;						// PDO database handle. Possible database conenction held open for duration of program
	var $font = array();					// default font size

	var $parent = FALSE;					// parent window php object;
	

	function add_control($ctrl_obj,$event_obj=FALSE) {

		$name = $ctrl_obj->name;
		$this->control[$name] = $ctrl_obj;
		
	}
	

	function add_event($handler,$id) {
	
		if ( ! is_array($handler) ) {
			$handler = array($this,$handler);
		}


	}

	/**
	*	Destroy this window
	*	
	*	@param	
	*	@return	
	*	
	*/
	function destroy() {
        gtk::main_quit();	

	}

	function help($idx=FALSE) {
/*
		if ( $idx ) {
			base::help_show($idx);
		}
		else {
			base::help_show($this->win_name);
		}
*/
	}


	function cancel() {

		// Save Column widths
		$this->save_state();
		$this->save_size();
		$this->save_position();
        gtk::main_quit();	
	}

	function hide() {

		if ( $this->win_name == 'core' ) {
			return(FALSE);
		}
		elseif ( $this->standalone ) {
			$this->cancel();
		}
		else {
			$this->save_state();
			$this->save_size();
			$this->save_position();
			$this->win->hide();
			return(TRUE);
		}
	}
	

	function get_state() {
	
	}


	/*
	Called as soon as we have $this->win set
	*/
	function init_window($title=FALSE,$visible=TRUE) {

		if ( $title ) {
			$this->win->set_title($title);
		}

		$this->make_constants();
		$this->timers();				// override this method to disable
		$this->restore_size();
		$this->restore_position();

	}

	function visible($v=TRUE) {
		$this->win->set_visible($v);
	}

	function ipc_rx() {
	
	}
	
	function make_constants() {

		if ( ! is_dir(platform::app_data('Tazu-Timesheet') )) {
			mkdir(platform::app_data('Tazu-Timesheet'));
		}
		if(!defined('INIFILE')) 	define("INIFILE",		platform::app_data('Tazu-Timesheet')."config.ini");
		if(!defined('RECENTFILES')) define("RECENTFILES",	platform::app_data('Tazu-Timesheet')."recent.ini");
		if(!defined('SYSTEMSTATE')) define("SYSTEMSTATE",	platform::app_data('Tazu-Timesheet')."system.ini");

	}

	function onAbout() {
		$cnf = new conf_timesheet(INIFILE);


		$abt = new GtkAboutdialog;
		$abt->set_icon_from_file(PATH_RES.'app_icon_16.png');
		$abt->set_program_name(APPNAME);
		$abt->set_version(VERSION);
		
	    $pixbuf=GdkPixbuf::new_from_file(PATH_RES.'app_icon_32.png'); 
		$abt->set_logo($pixbuf);
		
		$abt->set_copyright('Pikaia Ltd. '.date('Y'));
		$abt->set_authors(
							array(
								'Mike Bennett <mike@pikaia.co.nz>',
							)
						);
		
		$comment = '';
		$comment .= "Management Console for Tazu-Timesheet\n";
		$comment .= "\n";
		$comment .= "\n";
		$comment .= sprintf("%s %s\n",platform::os(),platform::os_ver());

		$abt->set_comments($comment);
		$abt->set_website('http://pikaia.co.nz');

		$abt->run();		
		$abt->destroy();
		
	}
	
	function onCancel() {
		$this->cancel();
	}
	
	function onClose() {
		$this->cancel();
	}
	
	function onFeedback() {
	
		$cnf = new conf_timesheet(INIFILE);

	
		$body = sprintf("\n\nVersion:%s.%s\n",VER,BUILD);
	
		$cmd = sprintf("utils\MAPISend.exe");
		$params = sprintf("/E /S Tazu-Timesheets Manager Feedback /A office@pikaia.co.nz /B %s",$body);
		platform::exec($cmd,$params);		
	}
	
	function onHelp() {
		$this->help();
	}
	
	function onQuit() {
		$this->cancel();
	}
	
	function save() {
		
	}

	function save_state() {
	
	}

	function set_status($msg='',$idx=1) {
		if ( ! is_object($this->control['statusbar'.$idx]) ) return;
		$this->control['statusbar'.$idx]->set_text($msg);
	}
	
	function timers() {

		/* Inter process communication check */
		Gtk::timeout_add(1000, array($this, 'ipc_rx'));

	}

	function restore_position() {

		// Restores this window to previously saved position

		$cnf = new conf(SYSTEMSTATE);

		$x = $cnf->ini[strtoupper($this->win_name)]['x'];
		$y = $cnf->ini[strtoupper($this->win_name)]['y'];

		if ( $x == '' ) $x = 50;
		if ( $y == '' ) $y = 50;

		if ( $x > 10000 ) $x = 10;
		if ( $y > 10000 ) $y = 10;

		$this->win->move($x,$y);
			
	}


	function set_position($x,$y) {
		$this->win->move($x,$y);
	}
	

	
	function save_position() {


		$cnf = new conf(SYSTEMSTATE);

		// Restores this window to previously saved position
 		$cur_posn = $this->win->get_position();
//print_r($cur_posn);
//		$dim = $this->win->allocation;
//print_r($dim);


 		$cnf->ini[strtoupper($this->win_name)]['x'] = $cur_posn[0];
		$cnf->ini[strtoupper($this->win_name)]['y'] = $cur_posn[1];
		$cnf->save_ini();
	}
	

	function restore_size() {

		$cnf = new conf(SYSTEMSTATE);

		$w = $cnf->ini[strtoupper($this->win_name)]['w'];
		$h = $cnf->ini[strtoupper($this->win_name)]['h'];

		if ( ((int) $w > 50 )  && ((int) $h > 50 ) ) { 
			$this->win->resize($w,$h); 
		}
	
	}

	function save_size() {

		$cnf = new conf(SYSTEMSTATE);
		$dim = $this->win->allocation;

		$cnf->ini[strtoupper($this->win_name)]['w'] = $dim->width;
		$cnf->ini[strtoupper($this->win_name)]['h'] = $dim->height;
		$cnf->save_ini();
	}
	


}



?>